
var code ; //在全局 定义验证码
function createCode(){
	
code = new Array();
var codeLength = 4;//验证码的长度
var checkCode = document.getElementById("checkCode");
checkCode.value = "";

var selectChar = new Array(2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');

for(var i=0;i<codeLength;i++) {
   var charIndex = Math.floor(Math.random()*32);
   code +=selectChar[charIndex];
}
if(code.length != codeLength){
   createCode();
}
checkCode.value = code;
}



var resetUserId;

var code ; //在全局 定义验证码

/**
 * 生成验证码码
 */
function createCode(){
code = new Array();
var codeLength = 4;//验证码的长度
var checkCode = document.getElementById("checkCode");
checkCode.value = "";

var selectChar = new Array(2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');

for(var i=0;i<codeLength;i++) {
   var charIndex = Math.floor(Math.random()*32);
   code +=selectChar[charIndex];
}
if(code.length != codeLength){
   createCode();
}
checkCode.value = code;
}

/**
 * 根据登录名和手机验证用户
 */
function checkUser(){
	var userName=$("#userName").val();
	var mobile=$("#mobile").val();
	var inputCode = document.getElementById("inputCode").value.toUpperCase();
	
	if(isNull(userName)){
		resetpwd_errorTips("resetpwd_tips","用户名不能为空！");
		return;
	}
	
	if(isNull(mobile)||!isMobile(mobile)){
		resetpwd_errorTips("resetpwd_tips","电话号码非法");
		return;
	}

	if(inputCode.length <=0) {
		resetpwd_errorTips("resetpwd_tips","请输入验证码！");
	   return;
	}
	else if(inputCode != code ){
		resetpwd_errorTips("resetpwd_tips","验证码输入错误！");
	   	createCode();
	   	return;
	}
	
	
	
	var params={
			"userName":userName,
			"mobile":mobile
	};
	
	$("#nextStepBtn").html("正在验证...");
	
	DDF_AsyncRequest("POST",CTX+"/shop/Login/checkBindingMobile.do",params,'checkMobileCallBack(data)');
	
}

/**
 * 验证用户回调函数
 */
function checkMobileCallBack(data){
	$("#nextStepBtn").html("下一步");
	if(data["code"]==1){
		$("#resetpwdDiv").css("display","none");
		$("#resetPasswordDiv").css("display","block");
		resetUserId=data["userId"];
		
	}else{
		resetpwd_errorTips("resetpwd_tips","输入的用户名与绑定手机不匹配！");
	}
	
}

/**
 * 点击 重置密码 按钮
 */
function resetPwd(){
	$("#resetBtn").val("正在重置密码，请稍后...");
 	if(checkPwd()){
 		getValidate();//密码正则表达通过，则执行短信验证码 验证
 	} 
	
}

/**
 * 重置密码回调函数
 */
function resetPwdCallBack(data){
	$("#resetBtn").val("重设密码");
	if(data["code"]==1){
		window.location.href="resetpwd_success.jsp";
	}else{
		resetpwd_errorTips("resetPassword_tips","重置密码失败，请重试一次！");
	}
}

/**
 * 检测新密码
 */
function checkPwd(){
	var newPwd=$("#newPwd").val();
	var reNewPwd=$("#reNewPwd").val();
	
	if(!isNull(newPwd)){
		if(!isLoginPassword(newPwd)){
			resetpwd_errorTips("resetPassword_tips","密码为6-20位数字，字母，符号组合，请重新输入");
			return false;
		}
	}else{
		resetpwd_errorTips("resetPassword_tips","新密码不能为空");
		return false;
	}
	
	if(!isNull(reNewPwd)){
		if(!isLoginPassword(reNewPwd)){
			resetpwd_errorTips("resetPassword_tips","密码为6-20位数字，字母，符号组合，请重新输入");
			return false;
		}
	}else{
		resetpwd_errorTips("resetPassword_tips","确认新密码不能为空");
		return false;
	}
	
	if(newPwd!=reNewPwd){
		resetpwd_errorTips("resetPassword_tips","两次输入的密码不一致");
		return false;
	}
	
	return true;
	
}

//发送短信验证码
function getSMS() {

	var mobile = $("#mobile").val();
	
	if(isNull(mobile)||!isMobile(mobile)){
		resetpwd_errorTips("resetPassword_tips","电话号码非法");
		return;
	}
	
	var params={
				"mobile":mobile,
		};
	
	DDF_AsyncRequest("POST", CTX+"/shop/Login/sendValidateCode.do", params, "sendMessageCallBack(data);");
	
}


/**
 * 发送短信验证码回调函数
 */
function sendMessageCallBack(data) {
	if (data != null && data.code != "1") {
		resetpwd_errorTips("resetPassword_tips","短信验证码发送失败！");
		wait = 0;
		get_time();
	} else {
		get_time();
	}
}

//免费获取短信验证码的按钮设定时间
var wait = 90;
function get_time() {
	if (wait == 0) {
		$("#time_btn").attr("disabled", false);
		$("#time_btn").val("免费获取验证码");
		wait = 90;
	} else {
		$("#time_btn").attr("disabled", "disabled");
		$("#time_btn").val(wait + "秒后重新获取验证码");
		wait--;
		setTimeout(function() {
			get_time();
		}, 1000);
	}
}

//校验验证码是否正确
function getValidate() {
	
	var mobile = $("#mobile").val();
	var validatecode= $("#validatecode").val();
	
	if(isNull(mobile)||!isMobile(mobile)){
		resetpwd_errorTips("resetPassword_tips","电话号码非法");
		return;
	}
	
	if(isNull(validatecode)){
		resetpwd_errorTips("resetPassword_tips","短信验证码不能为空");
		return;
	}
	
	var params={
			"mobile":mobile,
			"validatecode":validatecode
	};
		
	DDF_AsyncRequest("POST",  CTX+"/shop/Login/getValidateCode.do",params, "getValidateCallBack(data);");
	
}

//校验验证码是否正确回调函数
var validateFlag=false;//用于判断短信验证码是否通过
function getValidateCallBack(data) {
	if (data != null && data.code != "1") {
		resetpwd_errorTips("resetPassword_tips","短信验证码不正确！");
	} else {
		
		//短信验证码通过，则执行重置密码
		var newPwd=$("#newPwd").val();
		var params={
	 				"userId":resetUserId,
	 				"newPwd":MD5(newPwd)
	 		};
	 		DDF_AsyncRequest("POST",CTX+"/shop/Login/resetPassword.do",params,'resetPwdCallBack(data)');
	}
}


/**
 * 错误提示
 */
function resetpwd_errorTips(_showDiv,_tip){
	if($("#"+_showDiv).attr("display")!="block"){
		$("#"+_showDiv).css("display","block");
	}
	var tipsdiv="<div class='reset_error'><b>&nbsp;</b>"+_tip+"</div>";	
	$("#"+_showDiv).html(tipsdiv);
}